package cn.edu.zzuli.nothinglink.controller;


import cn.edu.zzuli.nothinglink.anno.CurrentUser;
import cn.edu.zzuli.nothinglink.entity.MessageRecord;
import cn.edu.zzuli.nothinglink.entity.R;
import cn.edu.zzuli.nothinglink.service.MessageRecordService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suzyyy
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/message-record")
public class MessageRecordController {

    @Autowired
    MessageRecordService messageRecordService;

    @PostMapping("/put")
    public R putMessageRecord(@RequestBody MessageRecord messageRecord, @CurrentUser Integer stuId) {
        if (stuId == null
                || messageRecord.getMesTeaId() == null
                || StringUtils.isEmpty(messageRecord.getMesStuInfo())
                || messageRecord.getMesCourseId() == null)
            return R.error().add("error", "注意参数");

        return messageRecordService.addRecord(messageRecord.setMesStuId(stuId));
    }

    @GetMapping("/get/{isStu}")
    public R getMsgRecord(@PathVariable boolean isStu, @CurrentUser Integer id) {
        if (id == null) return R.error().add("error", "请先登陆");
        return messageRecordService.getMsgRecordBy(isStu, id);
    }

    @PutMapping("/reply")
    public R replyStuRecord(@RequestBody JSONObject json) {
        if (json.get("msgId") == null
                || json.get("content") == null
                || StringUtils.isEmpty(json.get("content")))
            return R.error().add("error", "参数有误");
        return messageRecordService.replyStu(json);
    }
}

